﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Coffee_Restaurant.DAL;
using Coffee_Restaurant.Database;
using MetroFramework.Controls;

namespace Coffee_Restaurant.UserControls
{
    public partial class ListTable : UserControl
    {
        private MetroTile metroTile1;

        //public delegate void ButtonEventHandler();
        public EventHandler<MyClass> AddFormTableDetails;
        public ListTable()
        {
            InitializeComponent();
        }

        public void LoadDataArea()
        {

            var start = DateTime.Now;
            panelContainer.Controls.Clear();
            int sizeTableNumber = Convert.ToInt32(ConfigurationManager.AppSettings["SizeTableNumber"]);
            var model = AreaDa.ListAreas();
            foreach (var area in model)
            {
                //khoi tao userControl Display khu vuc ban
                var displayAreaTable = new DisplayAreaTable();
                displayAreaTable.Dock = DockStyle.Top;
                displayAreaTable.lblNameArea.Text = area.Name + " (" + area.CountTable + ")";
                var modelTable = TableDa.TableByAreaId(area.AreaId);
                //#region set height auto flow item
                ////cach tinh so dong
                ////moi dong co 5 item
                ////moi item height 60 + 20 padding = 80
                ////total item / so item cua? dong + them 2 dong nua
                //int columnTable = Convert.ToInt32(ConfigurationManager.AppSettings["ColumnTable"]);
                //var line = (modelTable.Count / columnTable) + 2; // tinh xem bao nhieu hang
                //displayAreaTable.Height = line * (sizeTableNumber + 10); // nhan chieu cao theo so hang
                //#endregion

                //add table vao usercontrol
                foreach (var table in modelTable)
                {
                    displayAreaTable.flowLayoutPanel1.Controls.Add(AddTableNumber(table.Name));
                }
                panelContainer.Controls.Add(displayAreaTable);
            }
            Debug.WriteLine("LoadDataArea() " + (DateTime.Now - start).TotalSeconds);
        }
        public Button AddTableNumber(string name)
        {
            var start = DateTime.Now;
            // metroTile1
            // 
            metroTile1 = new MetroFramework.Controls.MetroTile();
            metroTile1.ActiveControl = null;
            metroTile1.Location = new System.Drawing.Point(11, 37);
            metroTile1.Name = "btntableNumber";
            metroTile1.Size = new System.Drawing.Size(70, 70);
            metroTile1.Text = "Bàn";
            metroTile1.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
            metroTile1.TileCount = int.Parse(name);
            metroTile1.Cursor = System.Windows.Forms.Cursors.Hand;
            metroToolTip1.SetToolTip(metroTile1, "Bàn số " + name);
            metroTile1.Click += metroTile1_Click;
            Debug.WriteLine("AddTableNumber() " + (DateTime.Now - start).TotalSeconds);
            return metroTile1;
        }

        void metroTile1_Click(object sender, EventArgs e)
        {
            var btn = sender as MetroTile;
            if (AddFormTableDetails != null) // neu event dc gan
            {
                AddFormTableDetails(sender, new MyClass { text = btn.TileCount.ToString() });
            }
        }

        private void ListTable_Load(object sender, EventArgs e)
        {
            //LoadDataArea();
        }

        public class MyClass : EventArgs
        {
            public string text { get; set; }
        }
    }
}
